/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    usart.c
 * @brief   This file provides code for the configuration
 *          of the USART instances.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "sensor_usart.h"

/* USER CODE BEGIN 0 */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
struct uart_rx_frame
{
    uint8_t buf[UART_RX_BUF_SIZE];              /* ֡���ջ��� */
    struct
    {
        uint16_t len    : 15;                               /* ֡���ճ��ȣ�sta[14:0] */
        uint16_t finsh  : 1;                                /* ֡������ɱ�־��sta[15] */
    } sta;                                                  /* ֡״̬��Ϣ */
};
static struct uart_rx_frame uart_485_1_rx_frame = {0};                                    
static struct uart_rx_frame uart_485_2_rx_frame = {0};
static uint8_t uart_485_1_tx_buf[UART_TX_BUF_SIZE]; 
//static uint8_t uart_485_2_tx_buf[UART_TX_BUF_SIZE];
//static uint8_t uart_tx_buf[UART_TX_BUF_SIZE];


/* USER CODE END 0 */

UART_HandleTypeDef huart4;
UART_HandleTypeDef huart3;

/* UART4 init function */
void MX_UART4_Init(void)
{

    /* USER CODE BEGIN UART4_Init 0 */

    /* USER CODE END UART4_Init 0 */

    /* USER CODE BEGIN UART4_Init 1 */

    /* USER CODE END UART4_Init 1 */
    huart4.Instance = UART4;
    huart4.Init.BaudRate = 9600;
    huart4.Init.WordLength = UART_WORDLENGTH_8B;
    huart4.Init.StopBits = UART_STOPBITS_1;
    huart4.Init.Parity = UART_PARITY_NONE;
    huart4.Init.Mode = UART_MODE_TX_RX;
    huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart4.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart4) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN UART4_Init 2 */

    /* USER CODE END UART4_Init 2 */

}

/* USART3 init function */

void MX_USART3_UART_Init(void)
{

    /* USER CODE BEGIN USART3_Init 0 */

    /* USER CODE END USART3_Init 0 */

    /* USER CODE BEGIN USART3_Init 1 */

    /* USER CODE END USART3_Init 1 */
    huart3.Instance = USART3;
    huart3.Init.BaudRate = 9600;
    huart3.Init.WordLength = UART_WORDLENGTH_8B;
    huart3.Init.StopBits = UART_STOPBITS_1;
    huart3.Init.Parity = UART_PARITY_NONE;
    huart3.Init.Mode = UART_MODE_TX_RX;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart3.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart3) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN USART3_Init 2 */

    /* USER CODE END USART3_Init 2 */

}

void HAL_SENSOR_MspInit(void)
{

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* USER CODE BEGIN UART4_MspInit 0 */

    /* USER CODE END UART4_MspInit 0 */
    /* UART4 clock enable */
    __HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);   // 使能接收中断
    __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);   // 使能空闲中断
    __HAL_UART_CLEAR_FLAG(&huart4, UART_FLAG_TC);
    __HAL_UART_CLEAR_FLAG(&huart4, UART_FLAG_IDLE);

    __HAL_RCC_UART4_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**UART4 GPIO Configuration
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX
     */
    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);
    /* USER CODE BEGIN UART4_MspInit 1 */

    /* USER CODE END UART4_MspInit 1 */

    /* USER CODE BEGIN USART3_MspInit 0 */

    /* USER CODE END USART3_MspInit 0 */
    /* USART3 clock enable */
    __HAL_RCC_USART3_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**USART3 GPIO Configuration
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
     */
    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* USART3 interrupt Init */
    HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART3_IRQn);

    /* USER CODE BEGIN USART3_MspInit 1 */
    __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE);   //�������ڽ����ж�
    __HAL_UART_ENABLE_IT(&huart3,UART_IT_IDLE);   /* ʹ��UART���߿����ж� */
    __HAL_UART_CLEAR_FLAG(&huart3,UART_FLAG_TC);
    __HAL_UART_CLEAR_FLAG(&huart3,UART_FLAG_IDLE);
    //__HAL_UART_ENABLE(&huart3);    HAL_UART_Init������ʹ��USART3

    /* USER CODE END USART3_MspInit 1 */
}

void HAL_SENSOR_MspDeInit(void)
{

    /* USER CODE BEGIN UART4_MspDeInit 0 */

    /* USER CODE END UART4_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_UART4_CLK_DISABLE();

    /**UART4 GPIO Configuration
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX
     */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);

    /* UART4 interrupt Deinit */
    HAL_NVIC_DisableIRQ(UART4_IRQn);
    /* USER CODE BEGIN UART4_MspDeInit 1 */

    /* USER CODE END UART4_MspDeInit 1 */

    /* USER CODE BEGIN USART3_MspDeInit 0 */

    /* USER CODE END USART3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART3_CLK_DISABLE();

    /**USART3 GPIO Configuration
    PB10     ------> USART3_TX
    PB11     ------> USART3_RX
     */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

    /* USART3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART3_IRQn);
    /* USER CODE BEGIN USART3_MspDeInit 1 */

    /* USER CODE END USART3_MspDeInit 1 */
}

void uart_485_1_printf(char *fmt, ...)
{
    va_list ap;
    uint16_t len;
    int t;

    va_start(ap, fmt);
    vsprintf((char *)uart_485_1_tx_buf, fmt, ap);
    va_end(ap);

    len = strlen((const char *)uart_485_1_tx_buf);
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);  //�ߵ�ƽ����
    for(t=0;t<len;t++)
    {
        while((USART3->SR&0X40)==0);			             //ѭ������,ֱ���������
        USART3->DR=uart_485_1_tx_buf[t];
    }
    while((USART3->SR&0X40)==0);
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);    //�͵�ƽ����
}

void uart_485_1_hex_send(struct SensorDataType _data)
{
    uint32_t t;
    for(t = 0; t < _data.len; t++){
                rt_kprintf("%02X ", _data.data[t]);
            }
            rt_kprintf("\n");
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);  //�ߵ�ƽ����
    //rt_thread_mdelay(1);
    for(t=0;t<_data.len;t++)
    {
        while((USART3->SR&0X40)==0);			             //ѭ������,ֱ���������
        USART3->DR=_data.data[t];
    }
    while((USART3->SR&0X40)==0);
    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);    //�͵�ƽ����
    //rt_thread_mdelay(1);
}

void uart_485_2_hex_send(struct SensorDataType _data)
{
    uint32_t t;
    for(t = 0; t < _data.len; t++){
            rt_kprintf("%02X ", _data.data[t]);
        }
        rt_kprintf("\n");
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_SET);  //�ߵ�ƽ����
    //rt_thread_mdelay(1);
    for(t=0;t<_data.len;t++)
    {
        while((UART4->SR&0X40)==0);			             //ѭ������,ֱ���������
        UART4->DR=_data.data[t];
    }
    while((UART4->SR&0X40)==0);
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_RESET);    //�͵�ƽ����
    //rt_thread_mdelay(1);
}
void uart_485_1_rx_restart(void)
{
    uart_485_1_rx_frame.sta.len     = 0;
    uart_485_1_rx_frame.sta.finsh   = 0;
}

void uart_485_2_rx_restart(void)
{
    uart_485_2_rx_frame.sta.len     = 0;
    uart_485_2_rx_frame.sta.finsh   = 0;
}
uint8_t *uart_485_1_rx_get_frame(void)
{
    if (uart_485_1_rx_frame.sta.finsh == 1)
    {
        uart_485_1_rx_frame.buf[uart_485_1_rx_frame.sta.len] = '\0';
        //if(uart_485_1_rx_frame.sta.len>0){
        //		uart_printf("\r\n&&len&&\r\n",uart_485_1_rx_frame.sta.len);   //debug��־
        //}
        return uart_485_1_rx_frame.buf;
    }
    else
    {
        return NULL;
    }
}
uint8_t *uart_485_2_rx_get_frame(void)
{
    if (uart_485_2_rx_frame.sta.finsh == 1)
    {
        uart_485_2_rx_frame.buf[uart_485_2_rx_frame.sta.len] = '\0';
        return uart_485_2_rx_frame.buf;
    }
    else
    {
        return NULL;
    }
}

uint16_t uart_485_1_rx_get_frame_len(void)
{
    if (uart_485_1_rx_frame.sta.finsh == 1)
    {
        return uart_485_1_rx_frame.sta.len;
    }
    else
    {
        return 0;
    }
}
uint16_t uart_485_2_rx_get_frame_len(void)
{
    if (uart_485_2_rx_frame.sta.finsh == 1)
    {
        return uart_485_2_rx_frame.sta.len;
    }
    else
    {
        return 0;
    }
}

/**
 * @brief       ATK-IDM750C UART��ʼ��
 * @param       baudrate: UARTͨѶ������
 * @retval      ��
 */


/**
 * @brief       ATK-IDM750C UART�жϻص�����
 * @param       ��
 * @retval      ��
 */
void USART3_IRQHandler(void)
{
    rt_kprintf("USART3_IRQHandler entered\n");
    uint8_t tmp;
    if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE) != RESET)        /* UART���չ��ش����ж� */
    {

        /* ֻ��ͨ���ȶ�SR�Ĵ������ٶ�DR�Ĵ�������������жϱ�־λ */
        (void)USART3->SR;                                  
        (void)USART3->DR;
    }

    if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_RXNE) != RESET)       /* UART�����ж� */
    {
        HAL_UART_Receive(&huart3,&tmp,1,1000);
        rt_kprintf("Received byte: %d\n", tmp);
        if (uart_485_1_rx_frame.sta.len < (UART_RX_BUF_SIZE - 1))           /* �ж�UART���ջ����Ƿ����
         * ����һλ��������'\0'
         */
        {
            uart_485_1_rx_frame.sta.finsh = 1;
            //uart_printf("1111&&%d&&",tmp);   //debug
            uart_485_1_rx_frame.buf[uart_485_1_rx_frame.sta.len] = tmp;         /* �����յ�������д�뻺�� */
            uart_485_1_rx_frame.sta.len++;                                      /* ���½��յ������ݳ��� */
        }
        else                                                                    /* UART���ջ������ */
        {
            uart_485_1_rx_frame.sta.len = 0;                                    /* ����֮ǰ�յ������� */
            uart_485_1_rx_frame.buf[uart_485_1_rx_frame.sta.len] = tmp;         /* �����յ�������д�뻺�� */
            uart_485_1_rx_frame.sta.len++;                                      /* ���½��յ������ݳ��� */
        }
        __HAL_UART_CLEAR_FLAG(&huart3,UART_FLAG_RXNE);
    }
    rt_kprintf("USART3_IRQHandler exited\n");

    if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET)       /* UART���߿����ж� */
    {
        uart_485_1_rx_frame.sta.finsh = 1;                                      /* ���֡������� */
        /* ֻ��ͨ���ȶ�SR�Ĵ������ٶ�DR�Ĵ�������������жϱ�־λ */
        (void)USART3->SR;
        (void)USART3->DR;
    }
}
void UART4_IRQHandler(void)
{   rt_kprintf("USART3_IRQHandler entered\n");
    uint8_t tmp;
    if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_ORE) != RESET)        /* UART���չ��ش����ж� */
    {
        /* ֻ��ͨ���ȶ�SR�Ĵ������ٶ�DR�Ĵ�������������жϱ�־λ */
        (void)UART4->SR;                                  
        (void)UART4->DR;
    }

    if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_RXNE) != RESET)       /* UART�����ж� */
    {
        HAL_UART_Receive(&huart4,&tmp,1,1000);
        rt_kprintf("Received byte: %d\n", tmp);
        if (uart_485_2_rx_frame.sta.len < (UART_RX_BUF_SIZE - 1))           /* �ж�UART���ջ����Ƿ����
         * ����һλ��������'\0'
         */
        {
            rt_kprintf("UART4 received byte: %02X at position %d\n", tmp, uart_485_2_rx_frame.sta.len);   //debug
            uart_485_2_rx_frame.buf[uart_485_2_rx_frame.sta.len] = tmp;         /* �����յ�������д�뻺�� */
            uart_485_2_rx_frame.sta.len++;                                      /* ���½��յ������ݳ��� */
        }
        else                                                                    /* UART���ջ������ */
        {
            uart_485_2_rx_frame.sta.len = 0;                                    /* ����֮ǰ�յ������� */
            uart_485_2_rx_frame.buf[uart_485_2_rx_frame.sta.len] = tmp;         /* �����յ�������д�뻺�� */
            uart_485_2_rx_frame.sta.len++;                                      /* ���½��յ������ݳ��� */
        }
        __HAL_UART_CLEAR_FLAG(&huart4,UART_FLAG_RXNE);
    }
    rt_kprintf("USART3_IRQHandler exited\n");
    if (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE) != RESET)       /* UART���߿����ж� */
    {
        rt_thread_mdelay(10);
        uart_485_2_rx_frame.sta.finsh = 1;                                      /* ���֡������� */
        rt_kprintf("UART4 frame finished, length: %d\n", uart_485_2_rx_frame.sta.len);
        (void)UART4->SR;
        (void)UART4->DR;
    }
}

/* USER CODE END 1 */
